package com.henghai.zhenxuan.ui.page.mall;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.ProductBean;
import com.henghai.zhenxuan.domain.request.AppListRequester;
import com.henghai.zhenxuan.ui.page.adapter.ProductListAdapter;
import com.henghai.zhenxuan.R;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

import java.util.ArrayList;

public class SearchFragment extends BaseFragment {

    private SearchStates mStates;
    private AppListRequester mAppListRequester;
    private ProductListAdapter mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(SearchStates.class);
        mAppListRequester = getFragmentScopeViewModel(AppListRequester.class);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mAppListRequester.getAppListResult().observe(this, httpBeanDataResult -> {
            WaitDialog.dismiss();
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.appList.set(httpBeanDataResult.getResult().getData().getRows());
        });
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new ProductListAdapter(getContext());
        mAdapter.setOnItemClickListener((i, productBean, i1) -> {
            Bundle bundle = new Bundle();
            bundle.putString("productId", productBean.getId() + "");
            nav().navigate(com.henghai.zhenxuan.R.id.action_search_to_Commodity, bundle);
        });
        return new DataBindingConfig(R.layout.fragment_search, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.listener, new ListenerHandler())
                .addBindingParam(BR.keylistener, new OnKeyListenerHandler());
    }

    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    public class OnKeyListenerHandler implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() ==
                    KeyEvent.ACTION_DOWN) {
                toggleSoftInput();
                mAppListRequester.request(getViewLifecycleOwner(), "", "", mStates.searchKey.get());
            }
            return false;
        }
    }


    public static class SearchStates extends StateHolder {
        public final State<String> searchKey = new State<>("");
        public final State<ArrayList<ProductBean.RowsBean>> appList = new State<>(new ArrayList<>());
    }
}
